
class PID():

    def __init__(self, Kp, Kd, Ki = 0):
        self.Kp = Kp
        self.Ki = Ki
        self.Kd = Kd

        self.last_value = 0
        self.value = 0
        self.value_D = 0
        self.value_I = 0
        self.out = 0

        self.max_value = 100

    def update(self, value):
        self.value = value
        self.value_D = value - self.last_value
        self.value_I += value
        if (abs(self.value_I) > self.max_value):
            self.value_I *= self.max_value/abs(self.value_I)
        self.last_value = value

        self.out = self.Kp * self.value + self.Kd * self.value_D + self.Ki * self.value_I
        
